export default {
    name: "GoodLists",
    data() {
        return {
            thisID: null,
            thisData: {},
            commentFilter: 0,
            commentList: [],
            queryInto: {
                pagenum: 1,
                pagesize: 6,
            },
            filterData: {
                good_id: null,
                overall_rating: null,
                has_img: null,
            },
            loading: false,
            finished: true,
            error: false,
            collect: null,
            show: false,
            good_count: 0,
            action: ""
        }
    },
    created() {
        this.thisID = this.$route.params.goodListsId;
        this.getGoodlists();
        this.$store.dispatch("GetConfig");
        this.getComment();
        this.getCollecte();
    },
    methods: {
        getGoodlists() {
            let api = this.$http.api.good.getByID(this.thisID);
            this.$http.callApi(api, {
                hasLoading: true
            }).then(res => {
                if (!res.data.attrs) res.data.attrs = [];
                res.data.sku = res.data.attrs.map(it => {
                    if (it.type == "dynamic") {
                        if (!it.value) {
                            it.value = [];
                        } else {
                            it.value = it.value.split(",");
                        }
                        it.selected_value = null;
                    }
                    return it;
                }).filter(it => it.type == "dynamic");
                this.thisData = res.data;
            })
        },
        getCollecte() {
            if (this.me && this.me.id) {
                let api = this.$http.api.collect.getCollect(this.thisID, this.me.id);
                this.$http.callApi(api).then(res => {
                    this.collect = res.data;
                });
            }
        },
        getComment() {
            this.loading = true;
            let pagenum = this.queryInto.pagenum;
            let pagesize = this.queryInto.pagesize;
            let api = this.$http.api.comment.getPage(pagenum, pagesize);
            this.filterData.good_id = this.thisID;
            this.$http
                .callApi(api, {
                    data: this.filterData,
                })
                .then((res) => {
                    this.commentList.push(...res.data.comment);
                    if (this.commentList.length >= res.data.total) {
                        this.finished = true;
                    } else {
                        this.queryInto.pagenum++;
                    }
                })
                .catch((err) => {
                    this.loading = false;
                    this.error = true;
                });
        },
        commentFilterChange(index) {
            this.commentFilter = index;
            if (index == 0) this.filterData.overall_rating = null;
            if (index == 1) this.filterData.overall_rating = [4, 5];
            if (index == 2) this.filterData.overall_rating = [3, 4];
            if (index == 3) this.filterData.overall_rating = [0, 3];
            if (index == 4) {
                this.filterData.has_img = 1;
                delete this.filterData.overall_rating
            } else {
                delete this.filterData.has_img;
            };
            this.finished = false;
            this.queryInto.pagenum = 1;
            this.commentList = [];
            this.getComment();
        },
        onContractClicked() {
            if (!this.isLogin) {
                this.$router.push("/login");
                return false;
            }
            location.href = "tel:" + this.config.server_tel;
        },
        onCollectClicked(type) {
            if (!this.isLogin) {
                this.$router.push("/login");
                return false;
            }
            let api;
            if (type == '收藏') {
                api = this.$http.api.collect.insert(this.thisID, this.me.id);
            } else {
                api = this.$http.api.collect.delete(this.collect.id);
            }
            this.$http.callApi(api).then(res => {
                this.collect = res.data;
                this.$toast.success(type + "成功");
            })
            this.getCollecte();
        },
        onPopupClicked(action) {
            if (this.isLogin) {
                this.show = true;
                this.action = action;
            } else {
                this.$router.push("/login");
            }
        },
        onSkuClicked(item, skuVal) {
            item.selected_value = skuVal;
        },
        onSubmit() {
            if (this.good_count == 0) {
                return this.$toast.fail("商品数量不能为0！");
            }
            for (let i = 0; i < this.thisData.sku.length; i++) {
                let item = this.thisData.sku[i];
                if (!item.selected_value) {
                    return this.$toast.fail("请选择" + item.name + "!");
                }
            }
            if (this.action == "cart") {
                let cartData = {
                    user_id: this.me.id,
                    freight_id: this.thisData.freight_id,
                    cat_one_id: this.thisData.cat_one_id,
                    cat_two_id: this.thisData.cat_two_id,
                    cat_three_id: this.thisData.cat_three_id,
                    good_id: this.thisID,
                    good_count: this.good_count,
                };
                cartData.good_sku = this.thisData.sku.map(it => {
                    return {
                        sku_name: it.name,
                        sku_value: it.selected_value,
                    }
                });
                let api = this.$http.api.cart.insert();
                this.$http.callApi(api, {
                    data: cartData,
                    hasLoading: true
                }).then(res => {
                    this.$toast.success("加入购物车成功！");
                    this.show = false;
                });
            }
            if (this.action == "buy") {
                console.log("立即购买");
            }
        }
    },
    computed: {
        me() {
            return this.$store.state.me;
        },
        config() {
            return this.$store.state.config;
        },
        isLogin() {
            return this.$store.getters.isLogin;
        },
    }
}